#
# Copyright 2023 The titan-search Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#[[kmcmake_cc_test(
        NAME custom_comparator_test
        SOURCES custom_comparator_test.cc
        DEPS turbo::turbo ${KMCMAKE_DEPS_LINK} ${GTEST_LIB} ${GTEST_MAIN_LIB}
        COPTS ${USER_CXX_FLAGS}
)]]
#[[
kmcmake_cc_test(
        NAME foo_test
        SOURCES foo_test.cc
        DEPS ${PROJECT}::foo ${KMCMAKE_DEPS_LINK} gtest gtest_main
        COPTS ${USER_CXX_FLAGS}
)

kmcmake_cc_test(
        NAME foo_doctest
        SOURCES foo_doctest.cc
        DEPS ${PROJECT}::foo ${KMCMAKE_DEPS_LINK} doctest
        COPTS ${USER_CXX_FLAGS}
)
]]

find_package(KTest REQUIRED)
find_package(CURL REQUIRED)

file(
        COPY www www2 www3 ca-bundle.crt image.jpg
        DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
)

kmcmake_cc_test(
        NAME test
        MODULE spica
        SOURCES test.cc
        LINKS
        CURL::libcurl
        KTest::ktest
        spica::spica_static
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)


if(SPICA_OPENSSL_SUPPORT)
    if (OPENSSL_VERSION VERSION_LESS "3.2.0")
        set(OPENSSL_X509_FLAG "-x509")
    else()
        set(OPENSSL_X509_FLAG "-x509v1")
    endif()
    message(STATUS "openssl include path ${OPENSSL_INCLUDE_DIR}")
    find_program(OPENSSL_COMMAND
            NAMES openssl
            HINTS ${OPENSSL_INCLUDE_DIR}/../tools/openssl
            REQUIRED
            )
    if(NOT OPENSSL_COMMAND)
        message(STATUS "not found openssl")
        find_program(OPENSSL_COMMAND
                NAMES openssl
                HINTS ${OPENSSL_INCLUDE_DIR}/../bin
                REQUIRED
                )
    else ()
        message(STATUS "found openssl ${OPENSSL_COMMAND}")
    endif ()
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa 2048
            OUTPUT_FILE key.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem
            COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key.pem
            OUTPUT_FILE cert.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa 2048
            OUTPUT_FILE rootCA.key.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024
            OUTPUT_FILE rootCA.cert.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa 2048
            OUTPUT_FILE client.key.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key client.key.pem
            COMMAND ${OPENSSL_COMMAND} x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial
            OUTPUT_FILE client.cert.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa -passout pass:test123! 2048
            OUTPUT_FILE key_encrypted.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key_encrypted.pem
            COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key_encrypted.pem
            OUTPUT_FILE cert_encrypted.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa -aes256 -passout pass:test012! 2048
            OUTPUT_FILE client_encrypted.key.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key client_encrypted.key.pem -passin pass:test012!
            COMMAND ${OPENSSL_COMMAND} x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial
            OUTPUT_FILE client_encrypted.cert.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
endif()

#add_subdirectory(fuzzing)